package com.itheima.travel.service.impl;

import com.itheima.travel.config.SnowflakeIdWorker;
import com.itheima.travel.constant.RedisConstant;
import com.itheima.travel.mapper.AffixMapper;
import com.itheima.travel.pojo.Affix;
import com.itheima.travel.pojo.AffixExample;
import com.itheima.travel.req.AffixVo;
import com.itheima.travel.service.AffixService;
import com.itheima.travel.service.RedisCacheService;
import com.itheima.travel.utils.BeanConv;
import com.itheima.travel.utils.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Service
public class AffixServiceImpl implements AffixService {

    @Autowired
    private AffixMapper affixMapper;
    //雪花算法
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    //缓存
    @Autowired
    private RedisCacheService redisCacheService;
    //本地上传路径
    @Value("upLoad.pathRoot")
    private String pathRoot;
    //服务器保存地址
    @Value("upLoad.webSite")
    private String webSite;

    //文件上传功能
    @Override
    public AffixVo upLoad(MultipartFile multipartFile, AffixVo affixVo) throws IOException {
        //判断文件是否为空，防止空指针异常
        if(EmptyUtil.isNullOrEmpty(multipartFile)){
            //若是空，结束
            return null;
        }
        //获取大图信息
        //TODO 莫名奇妙的两部，测试的时候看原因
        String businessType = affixVo.getBusinessType();
        //关联业务
        affixVo.setBusinessType(businessType);
        //获取上穿的真实名称
        String realName = multipartFile.getOriginalFilename();
        //根据.进行切割,后缀类型
        String suffix = realName.substring(realName.lastIndexOf("."));
        affixVo.setSuffix(suffix);
        //获取一个唯一表示，采用雪花算法，或则UUID
      //  String fileName = UUID.randomUUID().toString();
       String fileName = String.valueOf(snowflakeIdWorker.nextId());
        affixVo.setFileName(fileName);
        //构建访问路径
        String pathUrl = businessType + "/" + fileName + suffix;
        //判断文件夹是否存在
        File file = new File(pathRoot + businessType);
        if(!file.exists()){
            //不存在，则创建
            file.mkdir();
        }
        //将上传的文件改成file的路径地址
        file=new File(pathRoot+pathUrl);
        //将图片存到这个路径下
        multipartFile.transferTo(file);
        pathUrl=webSite+pathUrl;
        //设置对象的保存路径为：pathUrl
        affixVo.setPathUrl(pathUrl);
        int i = affixMapper.insert(BeanConv.toBean(affixVo, Affix.class));
        //并返回结果对象
        return affixVo;
    }

    //绑定对应的业务
    @Override
    public Boolean bindBusinessId(AffixVo affixVo) {
        //修改业务类型
        int i = affixMapper.updateByPrimaryKeySelective(BeanConv.toBean(affixVo, Affix.class));
        //如果修改成功则更新缓存
        if(i>0){
            //更新并删除redis中的信息
            //key值为AffixService:findAffixByBusinessId:拼接id
            String key= RedisConstant.AFFIXSERVICE_FINDAFFIXBYBUSINESSID+affixVo.getBusinessId();
            redisCacheService.deleListCache(key);
        }
        return i>0;
    }

    //根据条件查询
    @Override
    public List<AffixVo> findAffixBuBusinessId(AffixVo affixVo) {
       //为业务添加缓存
        //AffixService:findAffixByBusinessId:拼接BusinessId
        String key=RedisConstant.AFFIXSERVICE_FINDAFFIXBYBUSINESSID+affixVo.getBusinessId();
        //创建一个例子，条件
        AffixExample affixExample = new AffixExample();
        //创建一个规范
        AffixExample.Criteria criteria = affixExample.createCriteria().andBusinessIdEqualTo(affixVo.getBusinessId());
        //根据条件查询
        List<Affix> affixes = affixMapper.selectByExample(affixExample);
        //将查询回来的数据放入redis中并返回
        return  redisCacheService.listCache(()->{
            List<AffixVo> affixVos = BeanConv.toBeanList(affixes, AffixVo.class);
            return affixVos;
        },key);
    }
}
